   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author MAN
 */
public class ProductoDAO extends Model implements DaoGenerico {

    public boolean existe(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = getCon().prepareStatement("SELECT COUNT(*) FROM productos WHERE codigo_producto =?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public boolean existe1(Object o) throws SQLException {
        Integer id = (Integer) o;
        boolean sw = true;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sent = "select * from productos where codigo_producto like '%" + id.intValue() + "%'";
        try {
            pstmt = getCon().prepareStatement(sent);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getString(1).equals("0")) {
                    sw = false;
                }
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }
        System.out.print("Valor sw:" + sw);
        return sw;
    }

    public Object load(ResultSet rs) throws SQLException {

        Producto pr = new Producto();
        pr.setDescripcion(rs.getString(1));
        pr.setNombre(rs.getString(2));
        pr.setCodigo(rs.getInt(3));
        pr.setCantidad(rs.getInt(4));
        pr.setValor(rs.getInt(5));

        return pr;
    }

    public Object extrae(Object o) throws SQLException {
        Integer id = (Integer) o;

        Connection conex = getCon();

        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }
        Producto pr = new Producto();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = conex.prepareStatement("SELECT * FROM productos WHERE codigo_producto=?");
            pstmt.setInt(1, id.intValue());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                pr = (Producto) load(rs);

            } else {
            }
        } finally {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }
        }

        return pr;
    }

    public void crear(Object o) throws SQLException {
        Producto pr = (Producto) o;
        Connection conex = getCon();
        Integer id = new Integer(pr.getCodigo());
        if (existe(id)) {
            throw new SQLException("Ya existe esta producto");
        }
        PreparedStatement pstmt = null;

        try {

            pstmt = conex.prepareStatement("insert into productos values (?,?,?,?,?)");

            pstmt.setString(1, pr.getDescripcion());
            pstmt.setString(2, pr.getNombre());
            pstmt.setInt(3, pr.getCodigo());
            pstmt.setInt(4, pr.getCantidad());
            pstmt.setDouble(5, pr.getValor());
           
            pstmt.executeUpdate();

        } finally {
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
        }

    }

    public List extrae1(Object cod1) throws SQLException {
        Integer cod = (Integer) cod1;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Producto pr = null;
        List lista = new LinkedList();
        String sent = "select * from productos where codigo_producto like '%" + cod.intValue()+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        pr = (Producto) load(rs);
                        lista.add(pr);
                    }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

    
      public List extrae2(Object nom) throws SQLException {
        String  nom1 = (String) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Producto pr = null;
        List lista = new LinkedList();
        String sent = "select * from productos where nombre like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();
     
            
                    while (rs.next()) {
                        pr = (Producto) load(rs);
                        lista.add(pr);
                    }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public List listar() throws SQLException {
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Producto pr = null;
        List lista = new LinkedList();
        try {
            ps = (PreparedStatement) conex.prepareStatement("select * from productos ");
            rs = ps.executeQuery();

            while (rs.next()) {
                pr= (Producto) load(rs);
                lista.add(pr);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

    public boolean update(Producto pr) throws SQLException {
        Connection conex = getCon();
        Integer id = new Integer(pr.getCodigo());

        if (!existe(id)) {
            throw new SQLException("Producto no registrado " + pr.getCodigo());
        }
        PreparedStatement pstmt = null;
        boolean res = false;
        try {
            pstmt = conex.prepareStatement("Update productos set codigo_producto  = ?, descripcion = ?, nombre=?, cantidad=?, valor=?  where codigo_producto = ?");
            pstmt.setString(1, pr.getDescripcion());
            pstmt.setString(2, pr.getNombre());
            pstmt.setInt(3, pr.getCodigo());
            pstmt.setInt(4, pr.getCantidad());
            pstmt.setDouble(5, pr.getValor());
           
            pstmt.executeUpdate();


            int r = pstmt.executeUpdate();
            if (r != 0) {
                res = true;
            }
        } finally {
            if (pstmt != null) {
                pstmt.close();
            }
        }
        return res;
    }

  

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listarlista(List l) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Cliente find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
